home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 16 / AMIGAplus Sonderheft 16 (1998)(ICP)(DE)[!].iso / pd / anwendungen / xpk_source / examples / xsum.c < prev    next >
C/C++ Source or Header  |  1998-08-27  |  2KB  |  60 lines

  1. /* XSum.c - sums up all bytes in a compressed or uncompressed file
  2.  *
  3.  * This is a typical read-and-process xpk application. Try it out... XSum a
  4.  * file, then compress it and XSum it again. The result should be the same.
  5.  */
  6.  
  7. #include <proto/exec.h>
  8. #include <proto/dos.h>
  9. #include <proto/xpkmaster.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. extern struct Library *DOSBase;
  15. struct Library *XpkBase = NULL;
  16.  
  17. char errbuf[XPKERRMSGSIZE + 1], *outbuf = NULL;
  18. long outlen, outbuflen;
  19.  
  20. void end(char *text)
  21. {
  22.   if(outbuf)
  23.     FreeMem (outbuf, outbuflen);
  24.   if(text)
  25.     Write (Output (), text, strlen (text));
  26.   if(XpkBase)
  27.     CloseLibrary (XpkBase);
  28.   exit (text ? 10 : 0);
  29. }
  30.  
  31. void main(int argc, char *argv[])
  32. {
  33.   UBYTE *ptr, *last;
  34.   ULONG sum = 0;
  35.  
  36.   if(!(XpkBase = OpenLibrary (XPKNAME, 0)))
  37.     end("Cannot open " XPKNAME "\n");
  38.  
  39.   if(argc != 2)
  40.     end("Usage: XSum filename\n");
  41.  
  42.   if(XpkUnpackTags(
  43.     XPK_InName, argv[1],    /* The file name to be read              */
  44.     XPK_GetError, errbuf,    /* A pointer to the error message buffer */
  45.     XPK_GetOutBuf, &outbuf,    /* Sets a pointer to the output buffer   */
  46.     XPK_GetOutLen, &outlen,    /* Sets the number of bytes written      */
  47.     XPK_GetOutBufLen, &outbuflen,    /* Sets the length of the output buffer  */
  48.     XPK_PassThru, TRUE,    /* Will pass through uncompressed data   */
  49.     TAG_DONE))
  50.     end (strcat (errbuf, "\n"));
  51.  
  52.   ptr = (UBYTE *) outbuf;
  53.   last = ptr + outlen;
  54.   while (ptr < last)
  55.     sum += *ptr++;
  56.   printf ("%d\n", sum);
  57.  
  58.   end (NULL);
  59. }
  60.